package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ContractDao contractDao;

    /**
     * 购销合同分页
     * @param extCproductExample 分页查询的参数
     * @param pageNum 当前页
     * @param pageSize 页大小
     * @return
     */
    @Override
    public PageInfo<ExtCproduct> findByPage(ExtCproductExample extCproductExample, int pageNum, int pageSize) {
        //设置开始页与页面大小
        PageHelper.startPage(pageNum, pageSize);
        //查询全部
        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);
        PageInfo<ExtCproduct> pageInfo = new PageInfo<>(extCproductList);
        return pageInfo;
    }

    @Override
    public List<ExtCproduct> findAll(ExtCproductExample extCproductExample) {
        return extCproductDao.selectByExample(extCproductExample);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override// 保存附件
    public void save(ExtCproduct extCproduct) {

        extCproduct.setId(UUID.randomUUID().toString());
        //工厂创建时间
        extCproduct.setCreateTime(new Date());
        //工厂的更新时间
        extCproduct.setUpdateTime(new Date());
        //计算附件的总价
        double amount=0;
        if (extCproduct.getCnumber()!=null&&extCproduct.getPrice()!=null){
            amount=extCproduct.getCnumber()*extCproduct.getPrice();
            extCproduct.setAmount(amount);
        }
        extCproductDao.insertSelective(extCproduct);
        //计算出合同的总价
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        //计算出合同附件的数量
        if (contract.getExtNum()!=null){
            contract.setExtNum(contract.getExtNum()+1);
        }else {
            contract.setExtNum(1);
        }
        //更新合同表的数据
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override //更新附件
    public void update(ExtCproduct extCproduct) {
       //更新之前的附件对象
        ExtCproduct oldExtCproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        //附件的更新时间
        extCproduct.setUpdateTime(new Date());
        //计算附件总价
        double amount=0;
        if (extCproduct.getCnumber()!=null&&extCproduct.getPrice()!=null){
            amount=extCproduct.getCnumber()*extCproduct.getPrice();
            extCproduct.setAmount(amount);
        }
        //更新附件一条记录
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
        //计算合同的总价（原合同的总价-原附件总价+新附件总价）
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount()-oldExtCproduct.getAmount()+amount);

        //更新合同表的记录
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
       //删除之前下能找到附件
        ExtCproduct dbExtCproduct = extCproductDao.selectByPrimaryKey(id);
        //删除附件表的一条记录
        extCproductDao.deleteByPrimaryKey(id);
        //计算合同的总价（原合同的总价-附件总价）
        Contract contract = contractDao.selectByPrimaryKey(dbExtCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount()-dbExtCproduct.getAmount());
        //计算合同的附件数量
        contract.setExtNum(contract.getExtNum()-1);
        contractDao.updateByPrimaryKeySelective(contract);

    }

}
